package com.example.mobile2021606127cxz0614homework;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText edtMainName;
    private EditText edtMainPhone;
    private Button btnMainAdd;
    private Button btnMainDel;
    private Button btnMainXiugai;
    private Button btnMainAll;
    private Button btnMainOne;
    private TextView tvMainShow;
    MySQLiteOpenHelper openHelper;
    SQLiteDatabase database;
    private String name;
    private Long phone;
    private ContentValues values;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        openHelper = new MySQLiteOpenHelper(this, "phonebook.db", null, 1);
        btnMainAdd.setOnClickListener(this);
        btnMainDel.setOnClickListener(this);
        btnMainXiugai.setOnClickListener(this);
        btnMainAll.setOnClickListener(this);
        btnMainOne.setOnClickListener(this);
    }

    private void initView() {
        edtMainName = (EditText) findViewById(R.id.edt_main_name);
        edtMainPhone = (EditText) findViewById(R.id.edt_main_phone);
        btnMainAdd = (Button) findViewById(R.id.btn_main_add);
        btnMainDel = (Button) findViewById(R.id.btn_main_del);
        btnMainXiugai = (Button) findViewById(R.id.btn_main_xiugai);
        btnMainAll = (Button) findViewById(R.id.btn_main_all);
        btnMainOne = (Button) findViewById(R.id.btn_main_one);
        tvMainShow = (TextView) findViewById(R.id.tv_main_show);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_add:
                name = edtMainName.getText().toString().trim();
                phone = Long.parseLong(edtMainPhone.getText().toString().trim());
                values = new ContentValues();
                values.put("name", name);
                values.put("phone", phone);
                database = openHelper.getWritableDatabase();
                database.insert("userInfor", null, values);
                database.close();
                break;

            case R.id.btn_main_del:
                name = edtMainName.getText().toString().trim();
                database = openHelper.getWritableDatabase();
                database.delete("userInfor", "name=?", new String[]{name});
                database.close();
                break;

            case R.id.btn_main_xiugai:
                name = edtMainName.getText().toString().trim();
                phone = Long.parseLong(edtMainPhone.getText().toString().trim());
                values = new ContentValues();
                values.put("phone", phone);
                database = openHelper.getWritableDatabase();
                database.update("userInfor", values, "name=?", new String[]{name});
                database.close();
                break;

            case R.id.btn_main_all:
                database = openHelper.getReadableDatabase();
                Cursor cursor = database.query("userInfor", null, null, null, null, null, null);
                if (cursor.moveToFirst() || database.equals(null)) {
                    tvMainShow.setText(null);
                    while (cursor.moveToNext()) {
                        String name = cursor.getString(1);
                        int phone = cursor.getInt(2);
                        tvMainShow.append(name + "   " + phone + "   " + "\n");
                    }
                }
                cursor.close();
                database.close();
                break;

            case R.id.btn_main_one:
                String singlename = edtMainName.getText().toString().trim();
                database = openHelper.getReadableDatabase();
                Cursor cursor1 = database.query("userInfor", null, null, null, null, null, null);
                if (cursor1.moveToFirst() || database.equals(null)) {
                    tvMainShow.setText(null);
                    while (cursor1.moveToNext()) {
                        String name = cursor1.getString(1);
                        int phone = cursor1.getInt(2);
                        if (singlename.equals(name)) {
                            tvMainShow.append(name + "   " + phone + "   " + "\n");
                        }

                    }
                }
                cursor1.close();
                database.close();
                break;

        }
    }
}